package com.yuexunit.mobilenurse.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.yuexunit.mobilenurse.R;

import org.kymjs.kjframe.ui.ViewInject;

/**
 * Created by chenss on 2017/6/21 0021.
 */

public class LoginDialog {

    private Context context;
    private Dialog dialog;
    private Display display;

    EditText person_num;
    EditText person_psw;
    CheckBox auditSave;
    Button sure;
    Button cancel;

    /**
     * 一。内部接口，要求实现一个callback方法
     * @author Administrator cuiweiyou.com
     */
    private ICallBack icb;

    public LoginDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public LoginDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(
                R.layout.dialog_login, null);

        person_num = (EditText)view.findViewById(R.id.person_num);
        person_psw = (EditText)view.findViewById(R.id.person_psw);
        auditSave = (CheckBox)view.findViewById(R.id.auditSave);
        cancel = (Button)view.findViewById(R.id.cancel);
        sure = (Button)view.findViewById(R.id.sure);

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(icb != null){
                    // 这个方法在传入的回调器中实现
                    if(person_num.getText().toString().isEmpty()){
                        ViewInject.toast("请输入工号");
                        return;
                    }
                    if(person_psw.getText().toString().isEmpty()){
                        ViewInject.toast("请输入密码");
                        return;
                    }
                    Bundle bundle = new Bundle();
                    bundle.putString("person_num", person_num.getText().toString());
                    bundle.putString("person_psw", person_psw.getText().toString());
                    if(auditSave.isChecked()){
                        bundle.putString("auditSave", "1");
                    }else{
                        bundle.putString("auditSave", "0");
                    }
                    icb.callback(bundle);
                }
            }
        });

        // 设置Dialog最小宽度为屏幕宽度
        view.setMinimumWidth(display.getWidth());

        // 定义Dialog布局和参数
        dialog = new Dialog(context, R.style.loadingDialogStyle);
        dialog.setContentView(view);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);

        return this;
    }

    /**
     * 三。设置回调变量
     * @param cb
     */
    // 如果不是使用上面接收icallback参数的(二)构造方法创建此自定义对话框，则需要使用此方法指定icb变量
    public void setCallBack (ICallBack cb) {
        icb = cb;
    }

    public LoginDialog setTxt(String jobNum, String psw) {
        person_num.setText(jobNum);
        person_psw.setText(psw);
        auditSave.setChecked(true);
        return this;
    }

    public void show() {
        dialog.show();
    }

    public void dismiss() {
        dialog.dismiss();
    }

    public interface ICallBack {
        void callback(Bundle bundle);
    }
}
